#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

char linie[256],*c,fis[20][20];
int nr;

void afis_b(int i)
{
  int f=0,n,j=0;
  f = open (fis[i],O_RDONLY);
  while ((n = read (f,linie,256)) > 0)
    {
      j=0;
      nr++;
      if (linie[j] != '\n')
       fprintf (stderr, "\t%2d ",nr);
      while (j<strlen (linie))
       {
         if (linie[j] != '\n')
          if(linie[j-1] == '\n')
           {
            nr++;
            fprintf (stderr, "\t%2d ",nr);
           }
         fprintf (stderr, "%c",linie[j]);
         j++;
       }
    }
}
void afis_tot(int i,int ok1,int ok2,int ok3,int ok4)
{
  int f=0,n,j=0;
  f = open (fis[i],O_RDONLY);
  while ((n = read (f,linie,256)) > 0)
   {
    j=0;
    if (ok1 || ok3)
     nr++;
    if (( ok1 && linie[j] != '\n') || ok3)
     fprintf (stderr, "\t%2d ",nr);
    while (j<strlen(linie) && linie[j] != '\0')
     {
       if (ok2 && linie[j] == '\n')
        fprintf (stderr, "$");
       if (ok1)
        if (linie[j] != '\n')
         if (linie[j-1] == '\n')
          {
            nr++;
            fprintf (stderr, "\t%2d ",nr);
          }
       if (ok3 && linie[j-1] == '\n')
        {
         nr++;
         fprintf (stderr, "\t%2d ",nr);
        }
       if (ok4 && linie[j] == '\n' && linie[j+1] == '\n')
         while (linie[j] == '\n' && linie[j+1] == '\n')
          j++;
        fprintf (stderr, "%c",linie[j]);
       j++;
     }
   }
  close (f);
}

void afis_E(int i)
{
  int f=0,n,j=0;
  f = open (fis[i],O_RDONLY);
  while ((n = read (f,linie,256)) > 0)
   {
     j=0;
     while (j<strlen(linie))
      {
        if (linie[j] == '\n')
          fprintf (stderr, "$");
        fprintf (stderr, "%c",linie[j]);
        j++;
       }
   }
  close (f);
}

void afis_n(int i)
{
  int f=0,n,j=0;
  f = open (fis[i],O_RDONLY);
  while ((n = read (f,linie,256)) > 0)
   {
     j=0;
     nr++;
     fprintf (stderr, "\t%2d ",nr);
     while (j<strlen (linie))
      {
        if(linie[j] == '\n' && linie[j+1] != '\0')
          {
            nr++;
            fprintf (stderr, "\n\t%2d ",nr);
          }
        else
          fprintf (stderr, "%c",linie[j]);
        j++;
      }
   }
  close (f);
}

void afis_s(int i)
{
  int f=0,n,j=0;
  f = open (fis[i],O_RDONLY);
  while ((n = read (f,linie,256)) > 0)
    {
     j=0;
     while (j<strlen(linie))
      {
       if (linie[j] != '\n' && linie[j] != '\0')
        {
          fprintf (stderr, "%c",linie[j]);
          j++;
        }
       else if (linie[j] == '\n' && linie[j+1] == '\n' && linie[j+1] != '\0')
         {
           while (linie[j] == '\n' && j<strlen(linie))
            j++;
           fprintf (stderr, "\n\n");
         }
        else if (linie[j] == '\n' && linie[j+1] != '\n')
         {
          fprintf (stderr, "\n");
          j++;
         }
      }
   }
  close (f);
}

void calcul(int i,int ok1,int ok2,int ok3,int ok4)
{
  
  int f=0,n;
  if (ok1 && ok3)
    ok3 = 0; 
  afis_tot(i,ok1,ok2,ok3,ok4);
/*
  if (ok1)
   afis_b(i);
  if (ok2) 
   afis_E(i);
  if (ok3)
   afis_n(i);
  if (ok4)
   afis_s(i);
  if(!ok1 && !ok2 && !ok3 && !ok4)
   {
     f = open (fis[i],O_RDONLY);
     while ((n = read (f,linie,256)) > 0)
      fputs (linie, stderr);
     close (f);
   }*/
}

void cmd_cat(char comm[])
{
 int ok_b=0,ok_E=0,ok_n=0,ok_s=0,i=1,j;
 c = strtok (comm, " -");
 while ((c = strtok (NULL, " -")) != NULL)
  {
   if (strcmp (c, "b") == 0)
    ok_b = 1;
   else if (strcmp (c, "E") == 0)
    ok_E = 1;
   else if (strcmp (c, "n") == 0)
    ok_n = 1;
   else if (strcmp (c, "s") == 0)
    ok_s = 1;
   else
    {
     strcpy (fis[i],c);
     i++;
    }
  }
  i--;
  if(i)
   for (j=1;j<=i;j++)
     calcul (j,ok_b,ok_E,ok_n,ok_s);
  else
   do
    {
     fgets (linie,256,stdin);
    }
   while (!feof(stdin));
}

int main()
{
 char *comm = "cat -n -E ex1.txt";
 cmd_cat(comm);
 return 0;
}
